﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLiNhaSachBUS;

namespace QuanLiNhaSachGUI
{
    public partial class frmMain : Form
    {
        frmDangNhap FormDangNhap;
        List<Form> listForm = new List<Form>();

        public frmMain(frmDangNhap form)
        {
            InitializeComponent();
            FormDangNhap = form;
            this.ControlBox = false;
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDangNhap.Close();
        }

        private void frmMain_FormClosed(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
                f.Close();
            try
            {
                DatabaseHelper.CloseDataBase();
            }
            catch
            {
                throw new Exception("Lỗi khi đóng CSDL.");
            }
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDangNhap.Show();
            this.Close();
        }

        private void nhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiNhanVien && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiNhanVien form = new frmQuanLiNhanVien();
            form.Show();
            listForm.Add(form);
        }

        private void tácGiảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiTacGia && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiTacGia form = new frmQuanLiTacGia();
            form.Show();
            listForm.Add(form);
        }

        private void nhàXuấtBảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiNhaXuatBan && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiNhaXuatBan form = new frmQuanLiNhaXuatBan();
            form.Show();
            listForm.Add(form);
        }

        private void nhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiNhaCungCap && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiNhaCungCap form = new frmQuanLiNhaCungCap();
            form.Show();
            listForm.Add(form);
        }

        private void thểLoạiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiTheLoai && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiTheLoai form = new frmQuanLiTheLoai();
            form.Show();
            listForm.Add(form);
        }

        private void sáchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form f in listForm)
            {
                if (f is frmQuanLiSach && !f.IsDisposed)
                {
                    f.Focus();
                    return;
                }
            }

            frmQuanLiSach form = new frmQuanLiSach();
            form.Show();
            listForm.Add(form);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                DatabaseHelper.ConnectDataBase();
                //MessageBox.Show("Kết nối thành công");
            }
            catch
            {
                throw new Exception("Lỗi khi kết nối Cơ Sở Dữ Liệu");
            }
        }
    }
}
